package com.administrator.cloudapplication.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.administrator.cloudapplication.R;
import com.administrator.cloudapplication.constant.Urls;
import com.administrator.cloudapplication.models.NewsListModel;
import com.bumptech.glide.Glide;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/4/11.
 */

public class NewsListItemAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    Context mContext;
    List<NewsListModel.DataBean.NewsListBean> mListType = new ArrayList<>();
    String str;

    public NewsListItemAdapter(Activity activity, ArrayList<NewsListModel.DataBean.NewsListBean> mNewsListBean) {
        mListType = mNewsListBean;
        mContext = activity;
        mInflater = LayoutInflater.from(activity);
    }

    public int getCount() {
        return mListType.size();
    }

    @Override
    public NewsListModel.DataBean.NewsListBean getItem(int i) {
        if (mListType != null && mListType.size() != 0) {
            return mListType.get(i);
        }

        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    private class ViewHolder {
        ImageView img;
        TextView tv_top;
        TextView tv_source;
        TextView tv_time;
        TextView tv_read;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = mInflater.inflate(R.layout.view_pager_item, null);
            holder = new ViewHolder();
            holder.img = (ImageView) view.findViewById(R.id.vp_item_img);
            holder.tv_top = (TextView) view.findViewById(R.id.vp_item_top);
            holder.tv_source = (TextView) view.findViewById(R.id.vp_item_source);
            holder.tv_time = (TextView) view.findViewById(R.id.vp_item_tv_time);
            holder.tv_read = (TextView) view.findViewById(R.id.vp_item_tv_read);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        NewsListModel.DataBean.NewsListBean newsListModel = getItem(i);
        str = Urls.IMG_URL + newsListModel.getSubImage();
        if("".equals(newsListModel.getSubImage())){
            holder.img.setVisibility(View.GONE);
        }
        Glide.with(mContext).load(str).placeholder(R.mipmap.error).
                error(R.mipmap.error).into(holder.img);
        holder.tv_top.setText(newsListModel.getTitle());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(Long.valueOf(newsListModel.getMtime()) * 1000);
        String time = sdf.format(date);
        holder.tv_time.setText(time);
//        holder.tv_time.setText(newsListModel.getPubTime());
        holder.tv_read.setText(newsListModel.getViews());
        String eq=newsListModel.getSource();
        if(!"".equals(eq)){
            holder.tv_source.setText(eq);
        }
        else {
            holder.tv_source.setText("来源未知");
        }
        return view;
    }
}
